/**
 * Indexer.java 1.0 2007-6-7 上午01:10:18,创建 by yangfan
 */


package lia.ch1;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.in4j.core.common.TimeLogger;

import com.sohospace.lucene.analysis.xanalyzer.XFactory;

/**
 *
 * @author yangfan
 * @version 1.0 2007-6-7 上午01:10:18,创建
 */
public class Indexer
{
	private final String indexDirPath;
	private final String dataDirPath;

	public Indexer(String indexDirPath, String dataDirPath)
	{
		this.indexDirPath = indexDirPath;
		this.dataDirPath = dataDirPath;
	}

	public void execute() throws Exception
	{
		Directory indexDir = buildIndexDir(indexDirPath);
		IndexWriter writer = new IndexWriter(indexDir, getAnalyzer(), true);

		TimeLogger tl = new TimeLogger();

		indexDir(writer, new File(dataDirPath));

		writer.optimize();
		writer.close();
		int files = writer.docCount();

		System.out.println("共索引了 " + files + " 个文档,耗时:" + tl.getElapsedTime() + " ms.");
	}

	private void indexDir(final IndexWriter writer, File dataDir) throws IOException
	{
		File[] files = dataDir.listFiles(new FileFilter()
		{

			public boolean accept(File file)
			{
				if(file.isDirectory())
				{
					return true;
				}
				else
				{
					String fileName = file.getName();
					if(isDataFile(fileName))
						return true;
					return false;
				}
			}
		});

		for(File file: files)
		{
			if(file.isDirectory())
			{
				indexDir(writer,file);
			}
			else
			{
				String fileName = file.getName();
				if(fileName.endsWith(".mht") || fileName.endsWith(".txt")
						|| fileName.endsWith(".java")
						|| fileName.endsWith(".html"))
					indexFile(writer,file);
			}
		}
	}

	private void indexFile(IndexWriter writer, File file) throws IOException
	{
		if(!file.exists() || !file.canRead())
			return;
		System.out.println("Indexing " + file.getCanonicalPath());

		Document doc = new Document();
		Field text = new Field("text",new FileReader(file));
		doc.add(text);
		Field fileName = new Field("filename",file.getCanonicalPath(),Field.Store.YES,Field.Index.TOKENIZED);
		doc.add(fileName);
		for(int i = 0; i < 5; i++)
		{
			Field other = new Field("myname","yangfan" + i,Field.Store.YES,Field.Index.UN_TOKENIZED);
			doc.add(other);
		}
		writer.addDocument(doc);
	}

	private Analyzer getAnalyzer()
	{
		return XFactory.getWriterAnalyzer();
	}

	private Directory buildIndexDir(String path) throws IOException
	{
		FSDirectory directory = FSDirectory.getDirectory(path, true);
		return directory;
	}

	private boolean isDataFile(String fileName)
	{
		return fileName.endsWith(".mht") || fileName.endsWith(".txt");
	}

	public static void main(String[] args) throws Exception
	{
		if(args.length != 2)
			throw new Exception("使用方法: java " + Indexer.class.getName() + " <index dir> <data dir>");

		Indexer indexer = new Indexer(args[0], args[1]);
		indexer.execute();
	}
}
